package com.bitauto.i.commonUtils.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bitauto.i.commonUtils.config.ProductCheckHandlerConfig;
import com.bitauto.i.commonUtils.handler.AbstractCheckHandler;
import com.bitauto.i.commonUtils.handler.HandlerClient;
import com.bitauto.i.commonUtils.model.ProductVO;
import com.bitauto.i.commonUtils.model.Result;
import com.bitauto.i.commonUtils.service.ProductService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;

@Service
public class ProductServiceImpl implements ProductService {

    /**
     * 使用Spring注入:所有继承了AbstractCheckHandler抽象类的Spring Bean都会注入进来。Map的Key对应Bean的name,Value是name对应相应的Bean
     */
    @Resource
    private Map<String,  AbstractCheckHandler> handlerMap;

    @Override
    public ProductVO createProduct() {
        ProductVO data =new ProductVO(1L,"n95口罩","www.baidu.com", new BigDecimal(50),5);
        return data;
    }

    @Override
    public Result createCheck(ProductVO param) {
        return paramCheck(param);
        //return null;
    }

    private Result paramCheck(ProductVO param)  {

        //获取处理器配置：通常配置使用统一配置中心存储，支持动态变更
        ProductCheckHandlerConfig handlerConfig  =  this.getHandlerConfigFile();

        //获取处理器
        AbstractCheckHandler handler  =  this.getHandler(handlerConfig);

        //责任链：执行处理器链路
        Result  executeChainResult  =  HandlerClient.executeChain(handler,  param);
        if  (!executeChainResult.isSuccess())  {
            System.out.println("创建商品  失败...");
            return  executeChainResult;
        }

        //处理器链路全部成功
        return  Result.success();
    }

    private  ProductCheckHandlerConfig  getHandlerConfigFile()  {
        //配置中心存储的配置
        String  configJson  =  "{\"handler\":\"nullValueCheckHandler\",\"down\":true,\"next\":{\"handler\":\"priceCheckHandler\",\"next\":{\"handler\":\"stockCheckHandler\",\"next\":null}}}";
        //转成Config对象
        ProductCheckHandlerConfig  handlerConfig  =  JSONObject.parseObject(configJson,  ProductCheckHandlerConfig.class);
        return  handlerConfig;
    }

    /**
     *  获取处理器
     *  @param  config
     *  @return
     */
    private  AbstractCheckHandler  getHandler  (ProductCheckHandlerConfig  config)  {
        //配置检查：没有配置处理器链路，则不执行校验逻辑
        if  (Objects.isNull(config))  {
            return  null;
        }
        //配置错误
        String  handler  =  config.getHandler();
        if  (StringUtils.isEmpty(handler))  {
            return  null;
        }
        //配置了不存在的处理器
        AbstractCheckHandler  abstractCheckHandler  =  handlerMap.get(config.getHandler());
        if  (Objects.isNull(abstractCheckHandler))  {
            return  null;
        }

        //处理器设置配置Config
        abstractCheckHandler.setConfig(config);

        //递归设置链路处理器
        abstractCheckHandler.setNextHandler(this.getHandler(config.getNext()));

        return  abstractCheckHandler;
    }
}
